<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 五五 <15093565100@163.com>
// +----------------------------------------------------------------------
namespace plugins\aliyuncs\lib;
require_once __DIR__ . '/aliyun-oss-php-sdk-2.3.0.phar';

use OSS\Core\OssException;
use OSS\OssClient;

class Aliyuncs {

	private $config;

	private $storageRoot;

	private $endpoint;

	private $domain;

	/**
	 * @var \plugins\qiniu\QiniuPlugin
	 */
	private $plugin;

	/**
	 * Qiniu constructor.
	 * @param $config
	 */
	public function __construct($config) {
		$pluginClass = cmf_get_plugin_class('Aliyuncs');

		$this->plugin = new $pluginClass();
		$this->config = $this->plugin->getConfig();
		$domain = rtrim($this->config['domain'], "/");
		$domain = !empty($domain) ? preg_replace('/^http(s)?:\/\/(.*)\/?$/', '${2}', $domain) : "";
		$this->domain = empty($domain) ? $this->config['bucket'] . '.' . $this->config['endpoint'] : $domain;
		$this->storageRoot = $this->config['protocol']."://" . $this->domain . "/";
		$this->endpoint = $this->config['protocol'] . '://' . $this->config['endpoint'] . '/';
	}

	/**
	 * 文件上传
	 * @param string $file 上传文件路径
	 * @param string $filePath 文件路径相对于upload目录
	 * @param string $fileType 文件类型,image,video,audio,file
	 * @param array $param 额外参数
	 * @return mixed
	 */
	public function upload($file, $filePath, $fileType = 'image', $param = null) {
		$accessKeyId = $this->config['accessKey']; //"<您从OSS获得的AccessKeyId>";
		$accessKeySecret = $this->config['secretKey']; //"<您从OSS获得的AccessKeySecret>";
		$endpoint = $this->endpoint; //"<您选定的OSS数据中心访问域名，例如http://oss-cn-hangzhou.aliyuncs.com>";
		$bucket = $this->config['bucket']; //" <您使用的Bucket名字，注意命名规范>";
		$object = $file;
		$filePath = realpath($filePath);
		try {
			$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
			$res = $ossClient->uploadFile($bucket, $object, $filePath);
		} catch (OssException $e) {
			return $e->getMessage();
		}

		//上传后的地址
		$previewUrl = $fileType == 'image' ? $this->getPreviewUrl($file) : $this->getFileDownloadUrl($file);
		$url = $fileType == 'image' ? $this->getImageUrl($file, '') : $this->getFileDownloadUrl($file);

		return [
			'preview_url' => $previewUrl,
			'url' => $url,
		];
	}

	/**
	 * 获取图片预览地址
	 * @param string $file
	 * @param string $style
	 * @return mixed
	 */
	public function getPreviewUrl($file, $style = '') {
		$url = $this->getUrl($file, $style);

		return $url;
	}

	/**
	 * 获取图片地址
	 * @param string $file
	 * @param string $style
	 * @return mixed
	 */
	public function getImageUrl($file, $style = '') {
		$config = $this->config;
		$url = $this->storageRoot . $file;

		if (!empty($style)) {
			$url = $url . $config['style_separator'] . $style;
		}

		return $url;
	}

	/**
	 * 获取文件地址
	 * @param string $file
	 * @param string $style
	 * @return mixed
	 */
	public function getUrl($file, $style = '') {
		$config = $this->config;
		$url = $this->storageRoot . $file;

		if (!empty($style)) {
			$url = $url . $config['style_separator'] . $style;
		}

		return $url;
	}

	/**
	 * 获取文件下载地址
	 * @param string $file
	 * @param int $expires
	 * @return mixed
	 */
	public function getFileDownloadUrl($file, $expires = 3600) {
		$accessKeyId = $this->config['accessKey']; //"<您从OSS获得的AccessKeyId>";
		$accessKeySecret = $this->config['secretKey']; //"<您从OSS获得的AccessKeySecret>";
		$endpoint = $this->endpoint; //"<您选定的OSS数据中心访问域名，例如http://oss-cn-hangzhou.aliyuncs.com>";
		$bucket = $this->config['bucket']; //" <您使用的Bucket名字，注意命名规范>";
		$object = $file;
		$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
		$timeout = $expires; // URL的有效期是3600秒
		try {
			$signedUrl = $ossClient->signUrl($bucket, $object, $timeout);
		} catch (OssException $e) {
			// printf($e->getMessage() . "\n");
			return;
		}
		return $signedUrl;
	}

	/**
	 * 获取云存储域名
	 * @return mixed
	 */
	public function getDomain() {
		return $this->domain;
	}

	/**
	 * 获取文件相对上传目录路径
	 * @param string $url
	 * @return mixed
	 */
	public function getFilePath($url) {
		$parsedUrl = parse_url($url);

		if (!empty($parsedUrl['path'])) {
			$url = ltrim($parsedUrl['path'], '/\\');
			$config = $this->config;
			$styleSeparator = $config['style_separator'];

			$styleSeparatorPosition = strpos($url, $styleSeparator);
			if ($styleSeparatorPosition !== false) {
				$url = substr($url, 0, strpos($url, $styleSeparator));
			}
		} else {
			$url = '';
		}

		return $url;
	}
}